package com.pm2.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.pm2.dao.performance.PerformanceDao;
import com.pm2.entity.performance.Performance;
import com.pm2.entity.project.vo.MeetDetailVo;
import com.pm2.service.performance.PerformanceService;
import com.shuohe.util.date.SDate;
@Service
public class PerformanceServiceImpl implements PerformanceService {
	public static final Float  coefficient = 0.2f;
    @Autowired
    private PerformanceDao performanceDao;
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
	@Override
	public void saveOrUpdate(Performance t) {
		performanceDao.save(t);

	}
	
	/**
	 * 分开计算绩效
	 */
	@Override
	public void performanceCount(List<MeetDetailVo> list) {
		// TODO Auto-generated method stub
		String sql = "update db_pm2_meeting_detail SET performance_status = 'FINISH' where id in (";
		List<Performance> listp = new ArrayList<Performance>();
		for (int i=0;i<list.size();i++) {
			Performance p = new Performance();
			p.setName(list.get(i).getName());
			p.setPeriod(list.get(i).getStartDate()+"至"+list.get(i).getEndDate());
			p.setStartDate(list.get(i).getStartDate());
			p.setEndDate(list.get(i).getEndDate());
			p.setDoTime(SDate.getSystemDateToString());
			p.setTaskContent(list.get(i).getTaskContent());
			int countDate=0;
			try {
				countDate = SDate.daysBetween(list.get(i).getStartDate(), list.get(i).getEndDate());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			p.setDuration(countDate);
			Double taskP = (list.get(i).getEstimate_coefficient()/30)*countDate;//任务绩效
			BigDecimal performance = new BigDecimal(taskP);
			p.setTaskPerformance(performance);
			p.setCommitStatus("0");
			if(i==list.size()-1) {
				sql+=list.get(i).getId()+")";
			}else {
				sql+=list.get(i).getId()+",";
			}
			listp.add(p);
		}
		System.out.println(sql);
		performanceDao.save(listp);
		jdbcTemplate.execute(sql);

	}
//之前的计算多个绩效任务的方法
//	@SuppressWarnings("static-access")
//	@Override
//	public void performanceCount(List<MeetDetailVo> list) {
//		// TODO Auto-generated method stub
//		String sql = "update db_pm2_meeting_detail SET performance_status = 'FINISH' where id in (";
//		List<String> listDate = new ArrayList<String>();
//		Performance p = new Performance();
//		int count = list.size();
//		Double total = 0d;
//		String name = list.get(0).getName();
//		for (int i=0;i<list.size();i++) {
//			total+=list.get(i).getEstimate_coefficient();
//			listDate.add(list.get(i).getStartDate());
//			listDate.add(list.get(i).getEndDate());
//			if(i==list.size()-1) {
//				sql+=list.get(i).getId()+")";
//			}else {
//				sql+=list.get(i).getId()+",";
//			}
//		}
//		System.out.println(sql);
//		Map<String,String> map = SDate.showResult(listDate);
//		String minDate = map.get("minDate");
//		String maxDate = map.get("maxDate");
//		int countDate = 0;
//		try {
//			countDate = SDate.daysBetween(minDate, maxDate);
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		System.out.println("系数总和为==="+total);
//		BigDecimal b=new BigDecimal(total/count);
//		System.out.println("平均系数为===="+b);
//		p.setName(name);
//		p.setPeriod(minDate+"至"+maxDate);
//		p.setStartDate(minDate);
//		p.setEndDate(maxDate);
//		Double dou = ((total/count)/this.coefficient)*33.5*countDate;
//		BigDecimal performance = new BigDecimal(dou);
//		p.setPerformance(performance);
//		p.setDoTime(SDate.getSystemDateToString());
//		saveOrUpdate(p);
//		jdbcTemplate.execute(sql);
//
//	}

	@Override
	public void changeCommitStatus(int id, String status) {
		Performance p = performanceDao.findOne(id);
		p.setCommitStatus(status);
		performanceDao.save(p);
	}

}
